import 'package:sqflite/sqflite.dart';

import '../pojo/ImPojo.dart';
import '../util/DateUtil.dart';
import '../util/LoggerUtil.dart';
import '../util/MapUtil.dart';
import 'BaseDao.dart';

class ImFileDao extends BaseDao {

  final _logger = createLogger();

  Future<ImFile?> getById(int id) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List? list = await database?.rawQuery("select * from im_file where id = ? limit 1", [id]);
    if (list != null && list.isNotEmpty) {
      return Future.value(_toImFile(list[0]));
    }
    return Future.value(null);
  }

  Future<ImFile?> getByMd5AndUserId(String md5, String userId) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List? list = await database?.query("im_file", where: 'currentUserId = ? and md5 = ?', whereArgs: [userId, md5]);
    if (list != null && list.isNotEmpty) {
      return Future.value(_toImFile(list[0]));
    }
    return Future.value(null);
  }

  batchInserts(List<ImFile> imFiles) async {
    if (imFiles.isEmpty) return;

    Database? database = getDatabase(); //ImSingleton.getInstance().dataBase;
    for (var imFile in imFiles) {
      var valueBucket = _toInsertValueMap(imFile);
      int? id = await database?.insert("im_file", valueBucket);
      imFile.id = id;
    }
  }

  Future<List<ImFile>>  getFiles(int start, int size) async {
    Database? database = getDatabase(); //ImSingleton.getInstance()?.dataBase;
    List<Map<String, Object?>>? list = await database?.query("im_file",
        orderBy: 'createTime desc', offset: start, limit: size);
    return list != null ? list.map((map)=>_toImFile(map)).toList() : [];
  }

  Map<String, dynamic?> _toInsertValueMap(ImFile imFile) {
    Map<String, dynamic?> map = {};
    map['currentUserId'] = imFile.currentUserId;
    map['sessionId'] = imFile.sessionId;
    map['messageId'] = imFile.messageId;
    map['fileName'] = imFile.fileName;
    map['md5'] = imFile.md5;
    map['fileSize'] = imFile.fileSize;
    map['path'] = imFile.path;
    map['isRelativePath'] = imFile.isRelativePath ? 1 : 0;
    map['suffix'] = imFile.suffix;
    map["status"]= imFile.status?.value;
    map['createTime'] = DateUtil.formatDate(DateTime.now());
    map['updateTime'] = DateUtil.formatDate(DateTime.now());
    return map;
  }

  ImFile _toImFile(Map<String, dynamic> map) {
    var imFile = ImFile(
      currentUserId: MapUtil.getString(map, "currentUserId"),
      sessionId: MapUtil.getString(map, "sessionId"),
      messageId: MapUtil.getString(map, "messageId"),
      fileName: MapUtil.getString(map, "fileName"),
      md5: MapUtil.getString(map, "md5"),
      fileSize: MapUtil.getInt(map, "fileSize"),
      path: MapUtil.getString(map, "path"),
      isRelativePath: MapUtil.getBoolean(map, "isRelativePath"),
    );
    imFile.id = MapUtil.getInt(map, "id");
    imFile.suffix = MapUtil.getString(map, 'suffix');
    imFile.status = ImFileStatus.ofValue(MapUtil.getInt(map, "status"));
    imFile.createTime = MapUtil.getDate(map, "createTime");
    imFile.updateTime = MapUtil.getDate(map, "updateTime");
    return imFile;
  }

}